{{extend '../_layouts/home.html'}}

{{block 'title'}}{{'多人博客 - 首页'}}{{/block}}

{{block 'body'}}
<section class="container">
  {{include '../_partials/settings-admin.html'}}
  <div class="col-md-5">
    <div class="subhead">
      <h2>修改密码</h2>
    </div>
    <hr>
    <form id="update-password">
      <input type="hidden" name="_id" value="{{ user._id }}">
      <div class="form-group">
        <label for="exampleInputPassword1">当前密码</label>
        <input type="password" class="form-control" id="exampleInputPassword1" name="old" placeholder="">
      </div>
      <div class="form-group">
        <label for="new-password">新的密码</label>
        <input type="password" class="form-control" name="new" id="new-password" placeholder="输入新密码">
      </div>
      <div class="form-group">
        <label for="again-password">确认密码</label>
        <input type="password" class="form-control" name="password" id="again-password" placeholder="再次输入密码">
      </div>
      <button type="submit" class="btn btn-success">保存</button>
    </form>
    <div class="subhead">
      <h2>注销账号</h2>
      <hr>
    </div>
    <div>
      <p>一旦注销，不可找回，请谨慎操作</p>
      <a href="/delete"><button class="btn btn-danger" >删除</button></a>
    </div>
    <hr>
  </div>
</section>
{{/block}}

{{block 'script'}}
<script>
  $('#update-password').on('submit', function (e) {
    e.preventDefault(); // 处理默认提交行为
    const flag = $('#new-password').val() == $('#again-password').val() ? true : false;
    if (!flag) {
      return alert('请检查密码的一致性.')
    }



    const formData = $(this).serialize()
    $.ajax({
      url: '/updatePSW',
      type: 'post',
      data: formData,
      dataType: 'json',
      success: function (data) {
        var err_code = data.err_code

        if (err_code === 0) {
          alert('修改成功, 请重新登录');
          location.href = '/login';

        } else if (err_code === 1) {
          window.alert('邮箱或者密码错误')
        } else if (err_code === 500) {
          window.alert('服务器忙，请稍后重试！')
        }
      }
    })
  })
</script>
{{/block}}